package com.captain.leetcode.中级.数组;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/1/10 16:05
 */
public class 矩阵置零73 {
    public static void main(String[] args) {
        int[] m1 = {0, 1, 2, 0};
        int[] m2 = {3, 4, 5, 2};
        int[] m3 = {1, 3, 1, 5};
        int[][] matrix = new int[3][4];
        matrix[0] = m1;
        matrix[1] = m2;
        matrix[2] = m3;
        new 矩阵置零73().setZeroes(matrix);
    }

    public void setZeroes(int[][] matrix) {
        //行
        int m = matrix.length;
        //列
        int n = matrix[0].length;
        boolean[] row = new boolean[m];
        boolean[] col = new boolean[n];

        //第一次遍历找出行/列为0的
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 0) {
                    row[i] = true;
                    col[j] = true;
                }
            }
        }
        //将有0的行和列都置为0
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (row[i] || col[j]) {
                    matrix[i][j] = 0;
                }
            }
        }

    }
}
